package com.z.common.context;

import com.z.common.configure.security.LoginUser;
import com.z.sys.entity.Role;
import com.z.sys.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * 用户上下文
 * <p>
 * @serial 2.0.0
 * @author 就眠儀式
 */
@Component
public class UserContext {

    /**
     * 获取当前登录用户的信息，如果未登录返回null
     *
     * @return Object 当前登录用户
     */
    public static User currentUser() {
        return null!=getAuthentication()?(User) getAuthentication().getPrincipal():null;
    }

    /**
     * 获取当前登录用户的信息
     *
     * @return Authentication 权鉴对象
     */
    public static Authentication getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return !(authentication instanceof AnonymousAuthenticationToken)?authentication:null;
    }

    /**
     * 验证当前用户是否登录
     *
     * @return boolean 是否登录
     */
    public static boolean isAuthentication() {
        // if security session eq s-id is not null to index
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        return !(auth instanceof AnonymousAuthenticationToken);
    }

    /**
     *
     * @return 0 集团 1 单位 2 科室 3 班组 4 无权限
     */
    public static int getPowerLevel(String model) {
        if(StringUtils.isEmpty(model)){
            model = "人事";
        }
        LoginUser user = UserContext.getLoginUser();
        Set<String> roleNames = user.getRoleNames();
        if(user.getUserSn().equals("admin") || roleNames.contains("集团_"+model+"管理员")){
            return 0;
        } else if (roleNames.contains("单位_"+model+"管理员")){
            return 1;
        } else if (roleNames.contains("科室_"+model+"负责人")){
            return 2;
        } else if (roleNames.contains("班组_"+model+"负责人")){
            return 3;
        }
        return 4;
    }

    /**
     * User 当前用户
     * */
    public User getPrincipal(){
        try {
            return (User) getAuthentication().getPrincipal();
        } catch (NullPointerException e){
            return new User();
        }
    }
    /**
     * User 当前用户
     * */
    public static LoginUser getLoginUser(){
        try {
            return (LoginUser) getAuthentication().getPrincipal();
        } catch (NullPointerException e){
            return new LoginUser();
        }
    }

    /**
     * Username 当前用户名
     * */
    public String getUsername(){ return getPrincipal().getUsername(); }

    /**
     * nickname 当前用户昵称
     * */
    public String getRealName(){ return getPrincipal().getNickname(); }

    /**
     * UserId 用户编号
     * */
    public Long getUserId(){ return getPrincipal().getId(); }

    /**
     * deptId 当前部门
     * */
    public Long getDeptId(){ return getPrincipal().getDeptId(); }

    /**
     * roles 角色列表
     * */
    public List<Role> getRoles() { return getPrincipal().getRoles(); }
}
